package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PageQuery;
import com.itheima.service.EmpService;
import com.itheima.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
@RequiredArgsConstructor
public class EmpController {
    private final EmpService empService;

    //分页 根据条件查询
    @GetMapping
    public Result queryAllByCondition(PageQuery pageQuery) {
        PageBean pageBean = empService.queryAllByCondition(pageQuery);
        return Result.success(pageBean);
    }

    //添加员工
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("{}_param_{}", EmpController.class, emp);
        empService.addEmp(emp);
        return Result.success("操作成功");
    }

    //修改员工
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        empService.updateEmp(emp);
        return Result.success("");
    }

    //根据id查询
    @GetMapping(path = "/{id}")
    public Result queryEmpById(@PathVariable Integer id) {
        Emp emp = empService.queryEmpById(id);
        return Result.success(emp);
    }

    //根据id删除
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids) {
        empService.deleteByIds(ids);
        return Result.success("删除成功");
    }
}
